package com.ruoyi.system.outto.game.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.domain.InfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.mapper.AccountInfoMapper;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.model.mapper.SuprAuthMapper;
import com.ruoyi.system.outto.core.BQWConstan;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.game.BQWAPI;
import com.ruoyi.system.outto.game.CommonApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/5 16:19
 */
@Slf4j
@Service
public class BQWAPIImpl implements BQWAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Resource
    private AccountInfoMapper accountInfoMapper;
    @Resource
    private SuprAuthMapper suprAuthMapper;
    @Resource
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;

    static String Token = "fc25d908ec6111e5";
    static String ServiceName = "zhanghao";


    @Override
    public AjaxResult addGame(AccountInfo accountInfo, Long userId, SalePartner salePartner) {
        this.salePartner = salePartner;
        String url = "http://a.baqiwan1.com/docking/game/account";

//        http://a.baqiwan1.com/
        Map<String, Object> head = new HashMap<>();
        head.put("Token", salePartner.getSaleKey());
        head.put("Service-Name", salePartner.getOther());
//        head.put("Token", Token);
//        head.put("Service-Name", ServiceName);

        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(accountInfo, "add");
        log.info("霸气玩添加接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.doPostForm(salePartner.getTwoOther(), params, head);
        log.info("霸气玩添加接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"1".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
            JSONObject data = resultJson.getJSONObject("data");
            String res_id = data.getString("res_id");
            AccountInfo info = accountInfoMapper.selectAccountInfoById(accountInfo.getId());
            info.setBqwid(res_id);
            accountInfoMapper.updateAccountInfo(info);
        }

        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(AccountInfo accountInfo, Long userId, SalePartner salePartner) {
        this.salePartner = salePartner;
//        String url = "https://a.baqiwan1.com/docking/game/account";
        String url = "http://a.baqiwan1.com/docking/game/account";

        Map<String, Object> head = new HashMap<>();
        head.put("Token", salePartner.getSaleKey());
        head.put("Service-Name", salePartner.getOther());

//        head.put("Token", Token);
//        head.put("Service-Name", ServiceName);

        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(accountInfo, "edit");
        log.info("霸气玩编辑接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.doPostForm(salePartner.getTwoOther(), params, head);
        log.info("霸气玩编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);

        //{"code":1,"msg":"添加成功","time":1676002262,"data":{"res_id":"105042"}}

        if (!"1".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }

        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("msg"));
        return AjaxResult.success(result);
    }



    @Override
    public AjaxResult delGame(AccountInfo accountInfo, Long userId, SalePartner sa) {
        Map<String, Object> params = new TreeMap<>();
        params.put("account_id", accountInfo.getBqwid());
        params.put("act","del");
//        String url = "https://a.baqiwan1.com/docking/game/account";
        String url = "http://a.baqiwan1.com/docking/game/account";

        String path = "/docking/game/account";

        Map<String, Object> head = new HashMap<>();
        head.put("Token", sa.getSaleKey());
        head.put("Service-Name", sa.getOther());

//        head.put("Token", Token);
//        head.put("Service-Name", ServiceName);

        //sign
        String cover = MapUntils.getCover(params);
//        cover = cover + "key=" + Token;
        cover = cover + "key=" + sa.getSaleKey();
        String sign = DigestUtils.md5Hex(cover);
        params.put("sign", sign);
        log.info("霸气玩删除接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.doPostForm(sa.getTwoOther(), params, head);
        log.info("霸气玩删除接口返回参数={}", JSONObject.toJSONString(response));
        JSONObject resultJson = JSONObject.parseObject(response);
        Map<String, Object> result = new HashMap<>();

        if (!"1".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }

        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }


    private Map<String, Object> params(AccountInfo accountInfo, String type) {

        String des = accountInfo.getDes().replace("+","");

        accountInfo.setDes(descTitle + "||" + accountInfo.getId() + " " + accountInfo.getType() + " " + accountInfo.getLevel() + " " + des);

        Map<String, Object> params = new TreeMap<>();
        params.put("title", accountInfo.getDes());
        params.put("description", accountInfo.getDes());
        String titleImg = accountInfo.getTitleImg();
        if (StringUtils.isNotEmpty(titleImg)) {
            params.put("img", titleImg);
        } else {
            params.put("img", LCConstant.defaultImg);
        }
        InfoImg infoImg = new InfoImg();
        infoImg.setInfoId(accountInfo.getId());
        infoImg.setType(Constants.STR_1);
        List<InfoImg> infoImgs = infoImgMapper.selectInfoImgList(infoImg);

        String imgs = "";
        if (Objects.nonNull(accountInfo.getTitleImg())) {
            imgs = accountInfo.getTitleImg() + ",";
        }
        if (!CollectionUtils.isEmpty(infoImgs)) {
            imgs = imgs + infoImgs.stream().map(i -> i.getImgUrl()).collect(Collectors.joining(","));
        } else {
            imgs = imgs + LCConstant.defaultImg;
        }


        params.put("imgs", imgs);
        params.put("account_number", descTitle + "||" + accountInfo.getId());
//        String string = accountInfo.getPrice().stripTrailingZeros().toPlainString();
        BigDecimal bigDecimal = commonApi.addPrice(accountInfo.getPrice(), this.salePartner.getSeleep());
        commonApi.setPrice(bigDecimal,accountInfo.getId(),this.salePartner);
        params.put("price", bigDecimal.stripTrailingZeros().toPlainString());

        params.put("act", type);
//        params.put("account_id",accountInfo.getId());

        if (Objects.equals("英雄联盟", accountInfo.getType())) {
            params.put("game_id", 1);
            params.put("area", accountInfo.getRegional());
            JSONObject json = new JSONObject();
            List<String> ffs = new ArrayList<>();
            for (String str : BQWConstan.zzstrs) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            String join = StringUtils.join(ffs, ",");
            json.put("zz", join);


            for (String str : BQWConstan.hks) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("hks", join);

            for (String str : BQWConstan.rm) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");

            if (StringUtils.isNotEmpty(join)) {
                json.put("rm", join);
            }

            json.put("yxlm_yxsl", accountInfo.getHeroes());
            json.put("yxlm_pfsl", accountInfo.getSkins());
            params.put("data", json);
        }

        if (Objects.equals("王者荣耀", accountInfo.getType())) {
            params.put("game_id", 2);

            if (Objects.equals("安卓WX", accountInfo.getRegional())) {
                params.put("area", "安卓微信");
            }
            if (Objects.equals("安卓Q", accountInfo.getRegional())) {
                params.put("area", "安卓qq");
            }
            if (Objects.equals("苹果WX", accountInfo.getRegional())) {
                params.put("area", "苹果微信");
            }
            if (Objects.equals("苹果Q", accountInfo.getRegional())) {
                params.put("area", "苹果qq");
            }

            JSONObject json = new JSONObject();
            List<String> ffs = new ArrayList<>();
            for (String str : BQWConstan.yingxiong) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            String join = StringUtils.join(ffs, ",");
            json.put("yingxiong", join);

            for (String str : BQWConstan.shuijing) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("shuijing", join);

            for (String str : BQWConstan.xingyuan) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("xingyuan", join);

            for (String str : BQWConstan.xdpf) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("xdpf", join);

            for (String str : BQWConstan.chuanshuo) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("chuanshuo", join);

            String level = accountInfo.getLevel();
            if (Arrays.asList(LCConstant.haveTwoList).contains(level)) {
                json.put("bface", "包");
            } else {
                json.put("bface", "不包");
            }


            json.put("wzry_yxsl", accountInfo.getHeroes());
            json.put("wjmwsl", 0);
            json.put("gzdj", 0);
            json.put("gzjf", 0);
            params.put("data", json);
        }

        if (Objects.equals("CF端游", accountInfo.getType())) {
            params.put("game_id", 3);

            String[] cfRe = {"河南大区【北部】",
                    "北京大区【北部】",
                    "山东大区【北部】",
                    "河北大区【北部】",
                    "山西大区【北部】",
                    "辽宁大区【北部】",
                    "北方大区【北部】",
                    "吉林大区【北部】",
                    "黑龙江区【北部】",
                    "福建大区【东部】",
                    "安徽大区【东部】",
                    "上海大区【东部】",
                    "浙江大区【东部】",
                    "江苏大区【东部】",
                    "江西大区【南部】",
                    "南方大区【南部】",
                    "湖南大区【南部】",
                    "湖北大区【南部】",
                    "广东大区【南部】",
                    "广西大区【南部】",
                    "四川大区【西部】",
                    "重庆大区【西部】",
                    "云南大区【西部】",
                    "陕西大区【西部】"};

            for (String reStr : cfRe) {
                String re = reStr.split("-")[0];
                if (re.length() > 2) {
                    String substring = re.substring(0, 2);
                    if (accountInfo.getDes().contains(substring)) {
                        params.put("area", reStr);
                        break;
                    }
                }
            }


            JSONObject json = new JSONObject();
            List<String> ffs = new ArrayList<>();
            for (String str : BQWConstan.wzxjwq) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            String join = StringUtils.join(ffs, ",");
            json.put("wzxjwq", join);

            for (String str : BQWConstan.rmwq) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("rmwq", join);

            for (String str : BQWConstan.rmpf) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("rmpf", join);

            for (String str : BQWConstan.rmdj) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("rmdj", join);

            for (String str : BQWConstan.chuanshuo) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("chuanshuo", join);

            String level = accountInfo.getLevel();
            if (Arrays.asList(LCConstant.haveTwoList).contains(level)) {
                json.put("rcsm", "有");
            } else {
                json.put("rcsm", "无");
            }
            json.put("pwszg", "有");
            json.put("hero_weapon", accountInfo.getHeroes());
            params.put("data", json);
        }

        if (Objects.equals("原神", accountInfo.getType())) {
            params.put("game_id", 3);
            params.put("area", "米哈游");

            JSONObject json = new JSONObject();
            List<String> ffs = new ArrayList<>();
            for (String str : BQWConstan.wxyxa) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            String join = StringUtils.join(ffs, ",");
            json.put("wxyxa", join);

            for (String str : BQWConstan.wxwq) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("wxwq", join);

            params.put("data", json);
        }

        if (Objects.equals("和平精英", accountInfo.getType())) {

            params.put("game_id", 5);

            if (Objects.equals("安卓WX", accountInfo.getRegional())) {
                params.put("area", "安卓微信");
            }
            if (Objects.equals("安卓Q", accountInfo.getRegional())) {
                params.put("area", "安卓qq");
            }
            if (Objects.equals("苹果WX", accountInfo.getRegional())) {
                params.put("area", "苹果微信");
            }
            if (Objects.equals("苹果Q", accountInfo.getRegional())) {
                params.put("area", "苹果qq");
            }

            JSONObject json = new JSONObject();
            List<String> ffs = new ArrayList<>();
            for (String str : BQWConstan.zj) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            String join = StringUtils.join(ffs, ",");
            json.put("zj", join);

            ffs = new ArrayList<>();
            for (String str : BQWConstan.tx) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("tx", join);

            ffs = new ArrayList<>();
            for (String str : BQWConstan.yztz) {
                if (accountInfo.getDes().contains(str)) {
                    ffs.add(str);
                }
            }
            join = StringUtils.join(ffs, ",");
            json.put("yztz", join);

            if (Arrays.asList(LCConstant.haveTwoList).contains(accountInfo.getLevel())) {
                json.put("hpjy_bface", "包");
            } else {
                json.put("hpjy_bface", "不包");
            }
            json.put("hpjy_tzsl", accountInfo.getSuit());
            json.put("hpjy_qpsl", accountInfo.getGuns());
            json.put("hpjy_zjsl", 0);
            params.put("data", json);
        }

        if (Objects.equals("CF手游", accountInfo.getType())) {

            params.put("game_id", 7);

            if (Objects.equals("安卓WX", accountInfo.getRegional())) {
                params.put("area", "安卓微信");
            }
            if (Objects.equals("安卓Q", accountInfo.getRegional())) {
                params.put("area", "安卓qq");
            }
            if (Objects.equals("苹果WX", accountInfo.getRegional())) {
                params.put("area", "苹果微信");
            }
            if (Objects.equals("苹果Q", accountInfo.getRegional())) {
                params.put("area", "苹果qq");
            }

            JSONObject json = new JSONObject();
            json.put("wztz","");
            json.put("wzwqq","");
            json.put("xywqq","");
            json.put("yxtz","");
            json.put("jss","");
            json.put("yxk","");
            params.put("data", json);
        }

        if (Objects.equals("逆战", accountInfo.getType())) {

            params.put("game_id", 8);
            params.put("area", accountInfo.getRegional());

            JSONObject json = new JSONObject();
            json.put("tz","");
            json.put("js","");
            json.put("wq","");
            json.put("djcw","");
            params.put("data", json);
        }

        if (Objects.equals("火影忍者", accountInfo.getType())) {

            params.put("game_id", 9);

            if (Objects.equals("安卓WX", accountInfo.getRegional())) {
                params.put("area", "安卓微信");
            }
            if (Objects.equals("安卓Q", accountInfo.getRegional())) {
                params.put("area", "安卓qq");
            }
            if (Objects.equals("苹果WX", accountInfo.getRegional())) {
                params.put("area", "苹果微信");
            }
            if (Objects.equals("苹果Q", accountInfo.getRegional())) {
                params.put("area", "苹果qq");
            }

            JSONObject json = new JSONObject();
            json.put("s_renzhe","");
            json.put("a_jirenzhe","");
            params.put("data", json);
        }


        if (Objects.equals("QQ飞车手游", accountInfo.getType())) {

            params.put("game_id", 10);

            if (Objects.equals("安卓WX", accountInfo.getRegional())) {
                params.put("area", "安卓微信");
            }
            if (Objects.equals("安卓Q", accountInfo.getRegional())) {
                params.put("area", "安卓qq");
            }
            if (Objects.equals("苹果WX", accountInfo.getRegional())) {
                params.put("area", "苹果微信");
            }
            if (Objects.equals("苹果Q", accountInfo.getRegional())) {
                params.put("area", "苹果qq");
            }

            JSONObject json = new JSONObject();
            params.put("data", json);
        }
        if (StringUtils.isNotEmpty(accountInfo.getBqwid())){
            params.put("account_id", accountInfo.getBqwid());
        }else {
            params.put("account_id", "");
        }

        String cover = MapUntils.getCover(params);
        cover = cover + "key=" + salePartner.getSaleKey();
//        cover = cover + "key=" + Token;
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover);
        log.info("sign={}", sign);
        params.put("sign", sign);
        return params;
    }


    public static void main(String[] args) {

//        Token:fc25d908ec6111e5
//        Service-Name:zhanghao


        Map<String, Object> params = new HashMap<>();
        //游戏类型参数
        params.put("game_id", 3);


        String cover = MapUntils.getCover(params);
        //转换参数是空时，使用这个
//        cover = cover + "&key=" + Token;
        cover = cover + "key=" + Token;
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover);
        params.put("sign", sign);

        String gameLists = HttpUtils.sendGetBQW("https://a.baqiwan.com/docking/game/gameLists?sign=" + sign, null, Constants.UTF8, Token, ServiceName);
        System.out.println(gameLists);
        //游戏列表
        //{"code":1,"msg":"游戏列表","time":1675838558,"data":[{"id":1,"title":"英雄联盟"},{"id":2,"title":"王者荣耀"},{"id":3,"title":"穿越火线"},{"id":4,"title":"原神"},{"id":5,"title":"和平精英"},{"id":6,"title":"QQ飞车"},{"id":7,"title":"枪战王者"},{"id":8,"title":"逆战"},{"id":9,"title":"火影忍者"},{"id":10,"title":"QQ飞车手游"},{"id":12,"title":"炉石传说"},{"id":14,"title":"球球大作战"},{"id":16,"title":"第五人格"},{"id":19,"title":"使命召唤"},{"id":20,"title":"崩坏3"},{"id":21,"title":"绝地求生"},{"id":27,"title":"守望先锋"},{"id":30,"title":"坦克世界"},{"id":31,"title":"航海王"},{"id":32,"title":"QQ靓号"},{"id":34,"title":"大话西游手游"},{"id":35,"title":"魔兽怀旧服"},{"id":37,"title":"三国杀OL"},{"id":38,"title":"三国杀十周年"},{"id":39,"title":"三国杀移动版"},{"id":40,"title":"FIFA OL4"},{"id":41,"title":"NBA2K OL2"},{"id":42,"title":"APEX英雄"},{"id":43,"title":"永劫无间"},{"id":44,"title":"古剑奇谭"},{"id":45,"title":"超激斗梦境"},{"id":46,"title":"哈利波特：魔法觉醒"},{"id":47,"title":"LOL手游"},{"id":48,"title":"DNF"}]}

        //获取游戏
//        String url = "https://a.baqiwan.com/docking/game/getGameAreaAttrById";
//
//        String gameLists = HttpUtils.sendGetBQW(url, MapUntils.getCover(params), Constants.UTF8, Token, ServiceName);
//        System.out.println(gameLists);

    }

}
